home *** CD-ROM | disk | FTP | other *** search
- Path: nuscc.nus.sg!eng30403
- From: eng30403@leonis.nus.sg (Tey Chee Meng)
- Newsgroups: comp.lang.c
- Subject: char/int conversion and comparison
- Date: 23 Mar 1996 05:27:00 GMT
- Organization: NUS
- Message-ID: <4j0234$7hi@nuscc.nus.sg>
- NNTP-Posting-Host: @leonis.nus.sg
- X-Newsreader: TIN [version 1.2 PL2]
-
- I got confused by the conversion and comparison between int and
- char. Consider the following fragment of code :
-
- char foo1='a'; /* 1st line */
- int foo2='a'; /* 2nd line */
-
- if (foo2==foo1); /* 3rd line */
-
- Is this comparison guaranteed to evaluate to TRUE on all machines ?
-
- I have read that on different machines, char may be implemented
- as signed or unsigned. Hence if sizeof(int) > sizeof(char), a promotion
- of char to int may or may not extend the most significant bit.
-
- On the other hand, a conversion of int to char, (if sizeof(int) >
- sizeof(char) ) results in the more significant bits being truncated. In
- line 1 above, the int 'a' is truncated and converted to a char foo1.
- Subsequently, the char foo1 is promoted to an int in the comparison with
- foo2.
-
- Since we cant tell whether the sign bit is extended or not, is it
- wise to use such a comparison if we want to be portable ?
-
- Thank you.
-
- --
-
- Tey Chee Meng
-
- " Not to take extremes, the middle way is the best "
-
-